import os
import sys
import time
import warnings

import pandas as pd
from multiprocessing import Pool

sys.path.append('..')
warnings.filterwarnings('ignore')

from tools.setting import DATA_DIR
from API.glassnode import get_indicators, all_http

GLASSNODE_DATA_DIR = os.path.join(DATA_DIR, 'glassnode_indicators')
os.makedirs(GLASSNODE_DATA_DIR, exist_ok=True)

indicator_list = list(all_http.keys())

def update_one_indicator(indicator_name):
    _indicator_name = indicator_name.replace(' ', '_').replace('.', '_').replace('/', '_')
    file_name = os.path.join(GLASSNODE_DATA_DIR, f'{_indicator_name}.csv')
    if os.path.exists(file_name):
        df = pd.read_csv(file_name)
        if not df.empty:
            df = df.set_index('end_date')
            start = df.index[-1]
        else:
            df = pd.DataFrame()
            start = None
    else:
        df = pd.DataFrame()
        start = None
    update = get_indicators(indic_name=indicator_name, start_date=start)
    if update.empty:
        return
    df = pd.concat([df, update])
    df.sort_index(inplace=True)
    df.reset_index(inplace=True)
    df.drop_duplicates(inplace=True, subset=['end_date'])
    df.to_csv(file_name, index=False)

for indicator_name in indicator_list:
    update_one_indicator(indicator_name)
